package com.example.customrecordbar;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;

public class DrawView extends View {
	
	private OnBarMoveListener mOnBarMoveListener;
	private int screenWidth, screenHeight;
	/**
	 * 视频长度
	 */
	private int videoDuration = 100;
	
	private String[] times = { "00:00", "01:00", "02:00", "03:00", "04:00",
			"05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00",
			"12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00",
			"19:00", "20:00", "21:00", "22:00", "23:00", "24:00", };

	public DrawView(Context context) {
		super(context);
		init(null);
	}

	public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(attrs);
	}

	public DrawView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(attrs);
	}
	
	private void init(AttributeSet attrs) {
		Log.d("DrawView", "init");
		screenWidth = DeviceUtil.getScreenWidth(getContext());
		screenHeight = DeviceUtil.getScreenHeight(getContext());
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		Log.d("DrawView", "onMeasure");
		setMeasuredDimension(measureWidth(widthMeasureSpec), heightMeasureSpec);
	}

	private int measureWidth(int widthMeasureSpec) {
		Log.d("DrawView", "measureWidth");
		int measureMode = MeasureSpec.getMode(widthMeasureSpec);
		int measureSize = MeasureSpec.getSize(widthMeasureSpec);
		int result = getSuggestedMinimumWidth();
		switch (measureMode) {
			case MeasureSpec.AT_MOST:
			case MeasureSpec.EXACTLY:
				result = measureSize + screenWidth;
				break;
			default:
				break;
		}
		return result;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.d("DrawView", "onDraw");
		super.onDraw(canvas);
		// 创建画笔
		Paint p = new Paint();

		// 绘制进度条
		p.setColor(Color.GREEN);// 设置绿色
		p.setStyle(Paint.Style.FILL);// 设置填满
		Log.d("zr", "getWidth() = " + getWidth());
//		canvas.drawRect(0 - screenWidth/2f, 90, 10, 100, p);// 长方形
//		canvas.drawRect(0, 90, getWidth() - screenWidth/2, 100, p);// 长方形
		canvas.drawRect(0, 90, getWidth(), 100, p);// 长方形
		
		
		// 小时之间的间隔
		float lengthPerHour = (getWidth() - screenWidth) / 24f;

		//绘制每个时间间隔
		p.setColor(Color.RED);// 设置红色
		for (int i = 0; i < 25; i++) {
			float startX = lengthPerHour * i + screenWidth/2f;
			canvas.drawRect(startX - 5, 100, startX + 5, 120, p);// 长方形
			
			// 绘制居中显示的文字 
			Rect targetRect = new Rect((int) (startX - lengthPerHour / 2), 120, (int) (startX + lengthPerHour / 2), 140);  //文字显示区域
			Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
			paint.setStrokeWidth(3);
			paint.setTextSize(16);
			String testString;
			if (i % 12 == 0) {
				testString = times[i];
			} else {
				testString = "";
			}
			paint.setColor(Color.TRANSPARENT);
			canvas.drawRect(targetRect, paint);
			paint.setColor(Color.RED);
			FontMetricsInt fontMetrics = paint.getFontMetricsInt();
			// 转载请注明出处：http://blog.csdn.net/hursing
			int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
			// 下面这行是实现水平居中，drawText对应改为传入targetRect.centerX()
			paint.setTextAlign(Paint.Align.CENTER);
			canvas.drawText(testString, targetRect.centerX(), baseline, paint);
		}

//		layout(0 - screenWidth, getTop(), getWidth() - screenWidth, getTop() + getHeight());
	}

	int lastX, lastY;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		Log.d("DrawView", "onTouchEvent");
		Log.d("DrawView", "getLeft = " + getLeft());
		Log.d("DrawView", "getRight = " + getRight());
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			Log.d("zr", "ACTION_DOWN");
			lastX = (int) event.getRawX();
			lastY = (int) event.getRawY();

			break;

		case MotionEvent.ACTION_MOVE:
			Log.d("zr", "ACTION_MOVE");
			int dx = (int) event.getRawX() - lastX;
			int dy = (int) event.getRawY() - lastY;

			int top = getTop();
			int left = getLeft() + dx;
			int right = left + getWidth();


			if (left >= 0) {
				left = 0;
				right = getWidth();
			}
			
			if(right < screenWidth){
				right = screenWidth;
				left = right - getWidth();
			}
			
			layout(left, top, right, top + getHeight());
			lastX = (int) event.getRawX();
			lastY = (int) event.getRawY();
			
			float deltaX = (0 - left - screenWidth/2);
			Log.d("zr", "deltaX = " + deltaX);
			long currentTime = (long) deltaX *2*videoDuration / screenWidth;
			
			mOnBarMoveListener.OnBarMove(currentTime);
			break;
		case MotionEvent.ACTION_UP:
			Log.d("zr", "ACTION_UP");
			break;
		default:
			Log.d("zr", "default");
			break;

		}

		return true;
	}

	/**
	 * 设置视频播放进度，更改播放进度轴的位置
	 * @param progress
	 */
	public void setProgress(int progress) {
		Log.d("DrawView", "setProgress");
		int left = 0 - screenWidth/2 - (getWidth() - screenWidth) / 2 * progress/100;
		int top = getTop();
		layout(left, getTop(), left + getWidth(), top + getHeight());
	}
	
	/**
	 * 设置视频的长度
	 * @param duration
	 */
	public void setVideoDuration(int duration) {
		Log.d("DrawView", "setVideoDuration");
		this.videoDuration = duration;
	}
	
	public interface OnBarMoveListener {
		/**
		 * @param currentTime 当前选中时间
		 */
		void OnBarMove(long currentTime);
	}
	
	public void setOnBarMoveListener(OnBarMoveListener onBarMoveListener) {
		mOnBarMoveListener = onBarMoveListener;
	}

}
